package com.example.config;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 多业务数据库配置
 * 包含所有数据源、SqlSessionFactory、事务管理器和SqlSessionTemplate的配置
 * 
 * @author AI Assistant
 * @since 2025-01-08
 */
@Configuration
public class MultiDataSourceConfig {

    /**
     * 用户业务数据源
     */
    @Primary
    @Bean(name = "userDbDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.user-db")
    public DataSource userDbDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 订单业务数据源
     */
    @Bean(name = "orderDbDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.order-db")
    public DataSource orderDbDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 日志业务数据源
     */
    @Bean(name = "logDbDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.log-db")
    public DataSource logDbDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 用户业务数据源SqlSessionFactory
     */
    @Primary
    @Bean(name = "userDbSqlSessionFactory")
    public SqlSessionFactory userDbSqlSessionFactory(@Qualifier("userDbDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/user/*.xml"));
        sessionFactory.setTypeAliasesPackage("com.example.domain");
        
        // 设置MyBatis配置
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        sessionFactory.setConfiguration(configuration);
        
        // 设置MyBatis Plus全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        DbConfig dbConfig = new DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        dbConfig.setLogicDeleteField("deleted");
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicNotDeleteValue("0");
        globalConfig.setDbConfig(dbConfig);
        sessionFactory.setGlobalConfig(globalConfig);
        
        return sessionFactory.getObject();
    }

    /**
     * 订单业务数据源SqlSessionFactory
     */
    @Bean(name = "orderDbSqlSessionFactory")
    public SqlSessionFactory orderDbSqlSessionFactory(@Qualifier("orderDbDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/order/*.xml"));
        sessionFactory.setTypeAliasesPackage("com.example.domain");
        
        // 设置MyBatis配置
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        sessionFactory.setConfiguration(configuration);
        
        // 设置MyBatis Plus全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        DbConfig dbConfig = new DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        dbConfig.setLogicDeleteField("deleted");
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicNotDeleteValue("0");
        globalConfig.setDbConfig(dbConfig);
        sessionFactory.setGlobalConfig(globalConfig);
        
        return sessionFactory.getObject();
    }

    /**
     * 日志业务数据源SqlSessionFactory
     */
    @Bean(name = "logDbSqlSessionFactory")
    public SqlSessionFactory logDbSqlSessionFactory(@Qualifier("logDbDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/log/*.xml"));
        sessionFactory.setTypeAliasesPackage("com.example.domain");
        
        // 设置MyBatis配置
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        sessionFactory.setConfiguration(configuration);
        
        // 设置MyBatis Plus全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        DbConfig dbConfig = new DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        dbConfig.setLogicDeleteField("deleted");
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicNotDeleteValue("0");
        globalConfig.setDbConfig(dbConfig);
        sessionFactory.setGlobalConfig(globalConfig);
        
        return sessionFactory.getObject();
    }

    /**
     * 用户业务数据源事务管理器
     */
    @Primary
    @Bean(name = "userDbTransactionManager")
    public DataSourceTransactionManager userDbTransactionManager(@Qualifier("userDbDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 订单业务数据源事务管理器
     */
    @Bean(name = "orderDbTransactionManager")
    public DataSourceTransactionManager orderDbTransactionManager(@Qualifier("orderDbDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 日志业务数据源事务管理器
     */
    @Bean(name = "logDbTransactionManager")
    public DataSourceTransactionManager logDbTransactionManager(@Qualifier("logDbDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 用户业务数据源SqlSessionTemplate
     */
    @Primary
    @Bean(name = "userDbSqlSessionTemplate")
    public SqlSessionTemplate userDbSqlSessionTemplate(@Qualifier("userDbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     * 订单业务数据源SqlSessionTemplate
     */
    @Bean(name = "orderDbSqlSessionTemplate")
    public SqlSessionTemplate orderDbSqlSessionTemplate(@Qualifier("orderDbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     * 日志业务数据源SqlSessionTemplate
     */
    @Bean(name = "logDbSqlSessionTemplate")
    public SqlSessionTemplate logDbSqlSessionTemplate(@Qualifier("logDbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
